package com.label.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.label.common.CommonResult;
import com.label.entity.Patientlabels;
import com.label.entity.User;
import com.label.service.PatientlabelsService;
import com.label.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jianhua
 * @since 2022-03-27
 */
@Api
@RestController
@RequestMapping("/api/patientlabels")
public class PatientlabelsController {
    @Autowired
    PatientlabelsService patientlabelsService;
    @RequestMapping("/list")
    @ApiOperation("病人信息列表查询page")
    CommonResult list(ModelMap modelMap, Page page2, @ModelAttribute("patientlavels") Patientlabels patientlabels,Integer page,Integer pageSize){
        List<Patientlabels> list=patientlabelsService.getalllabels();
//        求list数组数
        int size= list.size();
        if (pageSize>size){
            pageSize=size;
        }
        if (pageSize!=0){
//            求最大页数，防止page越界
            int maxPage=size%pageSize==0?size/pageSize:size/pageSize+1;
            if (page>maxPage){
                page=maxPage;
            }
        }
//        当前第一条数据下标
        int curIdx=page>1?(page-1)*pageSize:0;
//        将当前页数据放进pagelist
        List pageList=new ArrayList();
        for (int i=0;i<pageSize&&curIdx+i<size;i++){
            pageList.add(list.get(curIdx+i));
        }
        page2.setCurrent(page).setSize(pageSize).setTotal(list.size()).setRecords(pageList);
        modelMap.addAttribute("page",page2);
        return CommonResult.success(page2);
    }
    @RequestMapping("/save")
    @ApiOperation("添加病人信息")
    CommonResult save(Patientlabels patientlabels){
        return CommonResult.success(patientlabelsService.saveOrUpdate(patientlabels));
    }
    //id查询
    @RequestMapping("/getByid")
    CommonResult getByid(Integer id){
        return CommonResult.success(patientlabelsService.getById(id));
    }

    //逻辑删除
    @RequestMapping("/del")
    CommonResult del(Integer id){
        Patientlabels patientlabels=patientlabelsService.getById(id);
//    user.setUserState("o");
        return CommonResult.success(patientlabelsService.updateById(patientlabels));
    }
    //批量删除
    @RequestMapping("/delByIds")
    CommonResult delByIds(Integer[] id){
        return CommonResult.success(patientlabelsService.removeByIds(Arrays.asList(id)));
    }
//    模糊查询
    @RequestMapping("/getBylike")
    @ApiOperation("病人信息模糊查询")
    CommonResult getBylike(ModelMap modelMap, Page page2,Integer page,Integer pageSize,@ModelAttribute("key") String key) {
        List<Patientlabels> list=patientlabelsService.getBylike(key);
//        求list数组数
        int size= list.size();
        if (pageSize>size){
            pageSize=size;
        }
        if (pageSize!=0){
//            求最大页数，防止page越界
            int maxPage=size%pageSize==0?size/pageSize:size/pageSize+1;
            if (page>maxPage){
                page=maxPage;
            }
        }
//        当前第一条数据下标
        int curIdx=page>1?(page-1)*pageSize:0;
//        将当前页数据放进pagelist
        List pageList=new ArrayList();
        for (int i=0;i<pageSize&&curIdx+i<size;i++){
            pageList.add(list.get(curIdx+i));
        }
        page2.setCurrent(page).setSize(pageSize).setTotal(list.size()).setRecords(pageList);
        modelMap.addAttribute("page",page2);
        return CommonResult.success(page2);
    }
}
